<?php
defined('SYSPATH') or die('Access error!');
/**
 * 操作问题的类
 * ============================================================================
 * 功能描述： 
 *      提问，问题列表，问题的详细页面，tag相关
 * ============================================================================
 * $Author: liuxudong $
 */
class Controller_Question extends Controller_Q2a_Main
{

    private $question;

    public function before()
    {
        parent::before();
        $this->question = Model_Q2a_Question::factory('q2a_question');
        //处理问题的分类
        $category_list = $this->get_date_as_array(Q2A_CATEGORY_ID, 'q2a');
        $this->template->assign('category_list', $category_list);
        I18n::set_current_file('question');
    }

    //根据分类列出所有问题
    public function action_list()
    {
        $this->is_allowed();
        $category_id = intval($this->request->param('id'));
        $data = array();
        $data['category_id'] = $category_id;
        $data['type'] = Q2A_ALL;
        $this->display_category_questions($data);
    }

    //未回答的问题
    public function action_unanswered()
    {
        $category_id = intval($this->request->param('id'));
        $data = array();
        $data['category_id'] = $category_id;
        $data['type'] = Q2A_UNANSWERED;
        $this->display_category_questions($data);
    }


    //已回答的问题
    public function action_answered()
    {
        $category_id = intval($this->request->param('id'));
        $data = array();
        $data['category_id'] = $category_id;
        $data['type'] = Q2A_ANSWERED;
        $this->display_category_questions($data);
    }

    //热门问题排序
    public function action_hot()
    {
        $category_id = intval($this->request->param('id'));
        $data = array();
        $data['category_id'] = $category_id;
        $data['type'] = Q2A_HOT_QUESTION;
        $this->display_category_questions($data);
    }

    //根据标签得到问题
    public function action_tag()
    {
        $id = intval($this->request->param('id'));
        $this->display_tag_questions($id);
    }

    public function display_category_questions($data)
    {
        $question_list_temp = $this->question->get_question_by_category_id($data['category_id'], $data['type']);
        if (empty($question_list_temp))
        {
            $this->request->redirect('question/list');
        }
        /* 获取问题的标签 */
        $hot_tags = $this->question->get_hot_question_tags($data['category_id']);
        $this->template->assign('hot_tags', $hot_tags);
        /* 得到本分类的详细信息 */
        $category_list = $this->get_cache_file('q2a');
        if (isset($category_list[$data['category_id']]))
        {
            $this->template->assign('category_detail', $category_list[$data['category_id']]);
        }
        //当前位置设置
        $category_id = !empty($data['category_id']) ? $data['category_id'] : Q2A_CATEGORY_ID;
        $this->get_category_position($category_id, 'q2a', 'question/list');
        //获取分类下的seo配置
        $this->set_seo($this->category_seo($data['category_id']));
        $this->template->assign('current_type', $data['type']);
        $this->display_questions($question_list_temp);
        $this->template->view('question_list');
    }

    public function display_tag_questions($tag_id)
    {
        $question_list_temp = $this->question->get_question_by_tag_id($tag_id);
        /* 根据tag_id 获取tag的详细资料 */
        $category_id = Model::factory('q2a_tag')->get_var(array('id' => $tag_id), 'category_id');

        //当前位置设置
        $this->get_category_position($category_id, 'q2a', 'question/list');
        //获取分类下的seo配置
        $this->set_seo($this->category_seo($category_id));

        /* 获取问题的标签 */
        $hot_tags = $this->question->get_hot_question_tags($category_id);
        $this->template->assign('hot_tags', $hot_tags);
        /* 得到本分类的详细信息 */
        $this->display_questions($question_list_temp);
        $this->template->view('question_tag_list');
    }

    // 标签和分类公用的显示列表方法
    public function display_questions($question_list_temp)
    {
        $question_list = array();
        if (count($question_list_temp['list']) > 0)
        {
            foreach ($question_list_temp['list'] as $value)
            {
                $value['timeformat'] = Date::floortime($value['add_time'], 2);
                $value['url'] = URL::site(Route::get('question')->uri(array('action' => 'detail', 'id' => $value['id'])));
                $question_list[] = $value;
            }
        }
        $question_page = $question_list_temp['page'];
        unset($question_list_temp);
        $this->template->assign('question_list', $question_list);
        $this->template->assign('page', $question_page);
    }

    /**
     *  浏览问题
     */
    public function action_detail()
    {
        $id = intval($this->request->param('id'));
        $question_detail = $this->question->get_question_by_id($id);
        //跳转到404
        if (empty($question_detail) || empty($question_detail['category_id']))
        {
            $this->show_404();
        }
        //获取本用户的其他问题
        $user_related_questions = $this->question->get_user_related_question($question_detail['user_id']);
        //获取相关问题，（根据分类）
        $category_related_questions = $this->question->get_category_related_questions($question_detail['category_id']);
        //浏览次数加1
        $this->question->add_view_count($id, $question_detail['view_count']);
        //获取问题的答案
        $answer = Model_Q2a_Answer::factory('q2a_answer');
        $answer_list = $answer->get_answer_by_parent_id($question_detail['id']);

        //seo设置
        $keyword = array();
        if (Arr::validate_arr($question_detail['tags']))
        {
            foreach ($question_detail['tags'] as $value)
            {
                $keyword[] = $value['name'];
            }
        }
        $this->detail_seo($question_detail['category_id'], 'q2a', $question_detail['name'], $keyword, $question_detail['content']);
        //当前位置设置
        $this->get_category_position($question_detail['category_id'], 'q2a', 'question/list');
        $this->template->assign('answer_list', $answer_list['list']);
        $this->template->assign('page', $answer_list['page']);
        $page_num = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $this->template->assign('page_num', $page_num);
        $this->template->assign('category_related_questions', $category_related_questions);
        $this->template->assign('user_related_questions', $user_related_questions);
        $this->template->assign('data', $question_detail);
        $this->template->view('question_detail');
    }

    //提问
    public function action_ask()
    {
        //判断权限
        $this->is_allowed();

        if (!$this->logged_in())
        {
            Message::error('请先登录!');
            $this->request->redirect('user/login');
        }
        //获取用户的剩余积分
        $this->template->assign('user_account', $this->user_detail['points']);
        if ($_POST)
        {
            $data = array();
            $data['name'] = HTML::get_safe_content(Arr::get($_POST, 'name', ''));
            $data['content'] = HTML::get_safe_content(Arr::get($_POST, 'content', ''));
            $data['points'] = Arr::get($_POST, 'points', 0);
            $data['category_id'] = Arr::get($_POST, 'category_id', 0);
            if (!$this->question->validate($data))
            {
                Message::error(__('validate error'));
                $this->request->redirect('question/ask');
            }
            if (intval($data['points']) != 0)
            {
                if ($data['points'] > $this->user_detail['points'])
                {
                    Message::error(__('points error'));
                    $this->request->redirect('question/ask');
                }
            }
            //插入问题
            $question_id = $this->question->insert($data);
            //处理用户的积分
            $this->save_points($this->user_detail['id'], array('q2a_reward_requrie' => $data['points']), $question_id);
            //插入标签
            $tags = HTML::get_safe_content(Arr::get($_POST, 'tag', ''));
            $tags_id_array = array();
            if ($tags != '')
            {
                $tags_id_array = $this->question->insert_tag($tags, $data['category_id']);
            }
            //插入问题和标签的关联数据
            if (count($tags_id_array) > 0)
            {
                $this->question->insert_post_tag($question_id, $tags_id_array);
            }

            //处理用户的提问设置积分
            if ($this->settings['user_q2a_ask_points'] != 0)
            {
                //处理用户的积分
                $this->save_points($this->user_detail['id'], 'user_q2a_ask_points', $question_id);
            }
            Message::success(__('question add success'));
            $this->request->redirect('question/ask');
        }

        //页面的seo资料
        $this->set_seo(array('title' => __('ask a question')));
        //当前的位置
        $this->set_define_position(__('ask a question'));
        $this->template->view('question_ask');
    }

}

